<template>
  <!-- 批量设置分类 -->
  <el-dialog width="25%" v-el-drag-dialog title="批量设置分类" :visible.sync="visible">
    <div v-loading="loading" element-loading-text="保存中...">
      <el-form ref="form" :rules="rules" :model="entity" label-position="right" label-width="130px"
        style='margin-left:50px;'>
        <el-form-item label="零售商品分类:" prop="categoryCopyIds">
          <el-cascader v-model="entity.categoryCopyIds" :props="{ checkStrictly: true }" change-on-select
            placeholder="选择商品分类" :options="categoryData" class="special-input" clearable></el-cascader>
        </el-form-item>
        <!-- 如果是批发商品 goodsType = 31 判断是否使用别名-->
        <el-form-item label="批发商品分类：" prop="wholesaleGoodscategoryIds">
          <el-cascader v-model="entity.wholesaleGoodscategoryIds" placeholder="选择商品分类" :options="categoryData"
            class="default-input" :props="{ checkStrictly: true }" change-on-select filterable
            v-if="!enterpriseConfig.wholesaleCategoryAlias">
          </el-cascader>
          <el-cascader v-model="entity.wholesaleGoodscategoryIds" placeholder="选择商品分类" :options="wholesaleCategoryData"
            class="default-input" :props="{ checkStrictly: true }" change-on-select filterable v-else></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSave">确定</el-button>
        <el-button @click="handleCancel">取消</el-button>
      </div>
    </div>
  </el-dialog>
</template>
<script>
  import {
    listGoodsCategory,
    listWholesaleGoodsCategory
  } from '@/api/goods/goods'
  import {
    saveBatchSetGoodsCategory
  } from '@/api/goods/supplierGoods'
  import {
    getByEnterpriseId
  } from '@/api/user/enterprise'
  import {
    getToken
  } from '@/utils/auth'
  import elDragDialog from '@/directive/el-dragDialog'
  export default {
    props: {
      onSuccess: Function
    },
    directives: {
      elDragDialog
    },
    watch: {},
    data() {
      return {
        loading: false,
        // 系统参数
        enterpriseConfig: {
          categorySeparate: '0',
          wholesaleCategoryAlias: '0'
        },
        entity: {
          // 供应商商品id集合
          supplierGoodsIdList: [],
          // 商品分类
          categoryCopyIds: [],
          // 批发商品分类
          wholesaleGoodscategoryIds: []
        },
        visible: false,
        // 商品的所有分类json
        categoryData: [],
        // 批发商品类目
        wholesaleCategoryData: [],
        rules: {}
      }
    },
    created() {
      this.token = {
        accessToken: getToken()
      }
      this.initEnterpriseConfig()
      this.listGoodsCategory()
      this.listWholesaleGoodsCategory()
    },
    methods: {
      // 获取系统参数
      initEnterpriseConfig() {
        const enterpriseId = this.getEnterpriseId()
        getByEnterpriseId(enterpriseId).then((response) => {
          if (response.isError) return
          this.enterpriseConfig = response.data
        })
      },
      handleSave() {
        if (this.entity.categoryCopyIds.length <= 0 && this.entity.wholesaleGoodscategoryIds.length <= 0) {
          this.msgError('至少选择一种分类修改')
          return
        }
        var oneCategoryId = ''
        var twoCategoryId = ''
        var threeCategoryId = ''
        if (this.entity.categoryCopyIds.length > 0) {
          oneCategoryId = this.entity.categoryCopyIds[0]
          if (this.entity.categoryCopyIds.length > 1) {
            twoCategoryId = this.entity.categoryCopyIds[1]
          }
          if (this.entity.categoryCopyIds.length > 2) {
            twoCategoryId = this.entity.categoryCopyIds[1]
            threeCategoryId = this.entity.categoryCopyIds[2]
          }
        }
        var wholesaleOneCategoryId = ''
        var wholesaleTwoCategoryId = ''
        var wholesaleThreeCategoryId = ''
        if (this.entity.wholesaleGoodscategoryIds.length > 0) {
          wholesaleOneCategoryId = this.entity.wholesaleGoodscategoryIds[0]
          if (this.entity.wholesaleGoodscategoryIds.length > 1) {
            wholesaleTwoCategoryId = this.entity.wholesaleGoodscategoryIds[1]
          }
          if (this.entity.wholesaleGoodscategoryIds.length > 2) {
            wholesaleTwoCategoryId = this.entity.wholesaleGoodscategoryIds[1]
            wholesaleThreeCategoryId = this.entity.wholesaleGoodscategoryIds[2]
          }
        }
        const params = {
          supplierGoodsIds: this.entity.supplierGoodsIdList,
          enterpriseId: this.getEnterpriseId(),
          supplierId: this.getSupplierId(),
          oneCategoryId: oneCategoryId,
          twoCategoryId: twoCategoryId,
          threeCategoryId: threeCategoryId,
          wholesaleOneCategoryId: wholesaleOneCategoryId,
          wholesaleTwoCategoryId: wholesaleTwoCategoryId,
          wholesaleThreeCategoryId: wholesaleThreeCategoryId
        }
        this.loading = true
        saveBatchSetGoodsCategory(params).then(response => {
          this.loading = false
          if (response.isError) return
          this.handleCancel()
          this.msgSuccess('设置成功')
          this.onSuccess()
        })
      },
      handleCancel() {
        this.visible = false
        this.entity.supplierGoodsIdList = []
        this.entity.categoryCopyIds = []
        this.entity.wholesaleGoodscategoryIds = []
      },
      // 获取类目
      listGoodsCategory() {
        const enterpriseId = this.getEnterpriseId()
        listGoodsCategory(enterpriseId).then(response => {
          this.categoryData = response.data
        })
      },
      // 获取批发商品类目
      listWholesaleGoodsCategory() {
        const enterpriseId = this.getEnterpriseId()
        listWholesaleGoodsCategory(enterpriseId).then(response => {
          this.wholesaleCategoryData = response.data
        })
      },
      open(supplierGoodsIdList) {
        if (!supplierGoodsIdList || supplierGoodsIdList.length <= 0) {
          this.msgError('请选择商品')
          return
        }
        this.entity.supplierGoodsIdList = supplierGoodsIdList
        this.visible = true
      }
    }
  }
</script>

<style lang="less" scoped>

</style>
